package com.hou.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hou.EX.ServicesException.IdnotException;
import com.hou.mapper.GoodsMapper;
import com.hou.mapper.SeckillGoodsMapper;
import com.hou.pojo.Goods;
import com.hou.pojo.SeckillGoods;
import com.hou.service.deleteService;
import com.hou.vo.GoodsVo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @auoter:jackh
 */
@Service
@Slf4j
public class deleteServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements deleteService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    SeckillGoodsMapper seckillGoodsMapper;

    @Override
    public boolean save1(GoodsVo goodsVo) {
        Goods goods=new Goods();
        goods.setId(goodsVo.getId());
        goods.setGoodsName(goodsVo.getGoodsName());
        goods.setRate(goodsVo.getRate());
        goods.setAddMoney(goodsVo.getAddMoney());
        goods.setMaxMoney(goodsVo.getMaxMoney());
        goods.setDayMaxMoney(goodsVo.getDayMaxMoney());
        goods.setRiskLevel(goodsVo.getRiskLevel());
        goods.setValueDate(goodsVo.getValueDate());
        goods.setDueDate(goodsVo.getDueDate());
        goods.setSettlementMethod(goodsVo.getSettlementMethod());
        goods.setProductTerm(goodsVo.getProductTerm());
        goods.setGoodsTitle(goodsVo.getGoodsTitle());
        goods.setGoodsImg(goodsVo.getGoodsImg());
        goods.setGoodsTitle(goodsVo.getGoodsTitle());
        goods.setGoodsImg(goodsVo.getGoodsImg());
        goods.setGoodsDetil(goodsVo.getGoodsDetil());
        goods.setGoodsPrice(goodsVo.getGoodsPrice());
        goods.setGoodsStock(goodsVo.getGoodsStock());
        goods.setLimitday(goodsVo.getLimitday());


        SeckillGoods seckillGoods = new SeckillGoods();
        seckillGoods.setGoodsId(goodsVo.getId());
        seckillGoods.setSeckillPrice(goodsVo.getSeckillPrice());
        seckillGoods.setEndDate(goodsVo.getEndDate());
        seckillGoods.setStartDate(goodsVo.getStartDate());
        seckillGoods.setStockCount(goodsVo.getStockCount());
        seckillGoods.setGoodsId(goodsVo.getId());
        try {
            goodsMapper.insert(goods);
            //goodsVo.getSeckillPrice(),goodsVo.getGoodsStock(),goodsVo.getStartDate(),goodsVo.getEndDate()
            seckillGoodsMapper.insert(seckillGoods);
        }catch(Exception ex){
            log.info(ex.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public List<GoodsVo> select(String name) {
        List<GoodsVo> select1;
        try {
           select1 = goodsMapper.select(name);
        }catch(Exception ex){
            log.info(ex.getMessage());
            return null;
        }
        return select1;
    }

    @Override
    public List<GoodsVo> selectList() {
        List<GoodsVo> goods = goodsMapper.selectLists();
        return goods;
    }

    @Override
    public GoodsVo selectid(Long id) {
        GoodsVo selectid = goodsMapper.selectid(id);
        return selectid;
    }

    @Override
    public void updata(GoodsVo goodsVo) {
        Goods goods=new Goods();
        goods.setId(goodsVo.getId());
        goods.setGoodsName(goodsVo.getGoodsName());
        goods.setRate(goodsVo.getRate());
        goods.setAddMoney(goodsVo.getAddMoney());
        goods.setMaxMoney(goodsVo.getMaxMoney());
        goods.setDayMaxMoney(goodsVo.getDayMaxMoney());
        goods.setRiskLevel(goodsVo.getRiskLevel());
        goods.setValueDate(goodsVo.getValueDate());
        goods.setDueDate(goodsVo.getDueDate());
        goods.setSettlementMethod(goodsVo.getSettlementMethod());
        goods.setProductTerm(goodsVo.getProductTerm());
        goods.setGoodsTitle(goodsVo.getGoodsTitle());
        goods.setGoodsImg(goodsVo.getGoodsImg());
        goods.setGoodsTitle(goodsVo.getGoodsTitle());
        goods.setGoodsImg(goodsVo.getGoodsImg());
        goods.setGoodsDetil(goodsVo.getGoodsDetil());
        goods.setGoodsPrice(goodsVo.getGoodsPrice());
        goods.setGoodsStock(goodsVo.getGoodsStock());
        goods.setLimitday(goodsVo.getLimitday());


        SeckillGoods seckillGoods = new SeckillGoods();
        seckillGoods.setGoodsId(goodsVo.getId());
        seckillGoods.setSeckillPrice(goodsVo.getSeckillPrice());
        seckillGoods.setEndDate(goodsVo.getEndDate());
        seckillGoods.setStartDate(goodsVo.getStartDate());
        seckillGoods.setStockCount(goodsVo.getStockCount());
        seckillGoods.setGoodsId(goodsVo.getId());
        int updata = goodsMapper.updateById(goods);
        int integer = seckillGoodsMapper.updateById(seckillGoods);
        if (updata != 1&&integer!=1){
            throw new IdnotException("修改失败，可能是你的id值没有找到，请确定你的id值是否存在");
        }
    }

}
